﻿using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Threading;
using DNF.WCF.Service;

namespace WCF_ServiceHost
{
    class Program
    {
        static string SERVICE_BASE_ADDRESS = "http://localhost:8080/";

        static void Main(string[] args) // where T : IComparable<T>
        {
            new Thread(new ThreadStart(StartHelloWorldService)).Start();
                
            new Thread(new ThreadStart(StartCalculatorService)).Start();

            new Thread(new ThreadStart(StartBSTService<string>)).Start();
        }

        static void StartBSTService<T>() where T : IComparable<T>
        {
            string baseAddress = SERVICE_BASE_ADDRESS + "bst";

            StartAService(typeof(BSTService<T>), baseAddress);
        }

        static void StartCalculatorService()
        {
            string baseAddress = SERVICE_BASE_ADDRESS + "calculator";
            baseAddress = "http://localhost/servicemodelsamples/service.svc";
            //Uri baseAddress = new Uri("http://localhost:8080/hello");

            StartAService(typeof(CalculatorService), baseAddress);
        }

        static void StartHelloWorldService()
        {
            string baseAddress = SERVICE_BASE_ADDRESS + "hello";
            //Uri baseAddress = new Uri("http://localhost:8080/hello");

            StartAService(typeof(HelloWorldService), baseAddress);
        }

        // util method
        static void StartAService(Type type, string baseAddr)
        {
            Uri baseAddress = new Uri(baseAddr);

            // Create the ServiceHost.
            using (ServiceHost host = new ServiceHost(type, baseAddress))
            {
                // Enable metadata publishing.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                host.Description.Behaviors.Add(smb);

                // Open the ServiceHost to start listening for messages. Since
                // no endpoints are explicitly configured, the runtime will create
                // one endpoint per base address for each service contract implemented
                // by the service.
                host.Open();

                Console.WriteLine("The service {1} is ready at {0}", baseAddress, type.ToString());
                Console.WriteLine("Press <Enter> to stop the service.");
                Console.ReadLine();

                // Close the ServiceHost.
                host.Close();
            }
        }

        
    }
}
